const db = require('../../../db/index');
const myDate = require('../../tool/myDate');
const arrMethod = require('../../tool/arrMethod');
const mysql = require('mysql');

exports.addOneSalary = (id) => {
	// 获取现在的年月 xxxx-xx
	let dateObj = new Date();
	const arrMonth = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'];
	// 本年本月
	const dateStr = myDate.yearMonth(dateObj.getFullYear(), arrMonth[dateObj.getMonth()]);
	// 本年本月的上一月
	let dateYear = dateObj.getFullYear();
	let lastMonth = dateObj.getMonth() - 1;
	if (lastMonth < 0) {
		lastMonth = 11;
		dateYear - 1;
	};
	const lastDateStr = myDate.yearMonth(dateYear, arrMonth[lastMonth]);
	// 本年本月的下一月
	let nextMonth = dateObj.getMonth() + 1;
	if (nextMonth > 11) {
		nextMonth = 0;
		dateYear + 1;
	};
	const nextDateStr = myDate.yearMonth(dateYear, arrMonth[nextMonth]);
	const sql = 'select * from initial';
	db.query(sql, (err, results) => {
		const init_day = results[0].init_day;
		let obj = {
			s_user_id: id,
			s_office_day: 1,
			s_rest_day: 0,
			s_overtime_day: 0,
		}
		if(dateObj.getDate() >= init_day){
			obj.s_date = dateStr
			obj.s_all_day = myDate.getMonthDay(dateStr)
		} else {
			obj.s_date = lastDateStr
			obj.s_all_day = myDate.getMonthDay(lastDateStr)
		}
		// 获取岗位id，部门id
		const sql = 'select * from user where id=?'
		db.query(sql, id, (err, results) => {
			if (err) return console.log(err)
			obj.s_position_id = results[0].id_position
			obj.s_department_id = results[0].id_department1
			obj.s_post_salary = results[0].try_out_money
			const sql = 'insert into salary set ?'
			db.query(sql, obj, (err, results) => {
				if (err) return console.log(err)
				console.log('添加成功')
			})
		})
	})
}